import request from "@/axios";
import globle from "@/common/global"
import Vue from 'vue'
export default ({
    namespaced:true,
    state: {


        //主页推荐文章
        SquareArticle:[],

        SquareArticleInfo:{pageNo:1,pageSize:20,length:0,total:0},

        //搜索
        SearchData:[],
        

    },
    getters: {},
    mutations: {

       
        
        //设置广场文章信息
        SETSQUAREARTICLE(state,[data,total]){    
            state.SquareArticle = []  
            data.forEach((item,index)=>{
				item.img=item.img?.split(',')
                state.SquareArticle.push(item)
			})
    
            state.SquareArticleInfo.total=total
            state.SquareArticleInfo.length=state.SquareArticle.length
          

        },
        //修改分组  -> 样式兼容
        SETSQUAREARTICLEDEVISION(state,num){    
            state.SquareArticleInfo.devision=num
            let newData=globle.Devision(state.OldSquareArticle,num)
            state.SquareArticle=[]
            newData.forEach(item => state.SquareArticle.push(item))
        },
        //重置广场文章信息
        REFRRSHQUAREARTICLE(state){
            state.SquareArticle=[]
            state.SquareArticleInfo={pageNo:1,pageSize:20,length:0,total:0}
        },
        SETPAGE:(state,pageNo) =>{
            state.SquareArticleInfo.pageNo = pageNo
        }

         

    },
    actions: {

       //获取广场文章信息  
       async GetSquareArticle(context,data) {
       
          let {pageNo,pageSize} = context.state.SquareArticleInfo 
          context.state.SearchData = {...context.state.SearchData,...data}
          
  
          try{
            let {createdUid,type,classify,hot} = context.state.SearchData
            let res= await  request.get('/api/article/search',{params:{createdUid,type,classify,pageNo,pageSize,hot}})
      
            if(res.data.code==200)
                context.commit('SETSQUAREARTICLE',[res.data.data.records,res.data.data.total])
             return res.data

          }catch(error){return {code:400,message:error}}
       },
       ChangePageNo({commit,dispatch},data){
          commit('SETPAGE',data.newPageNo)
          dispatch('GetSquareArticle',data)
       },
       
       //刷新广场文章信息
       async RefreshSquareArticle(context,uid){
            context.commit('REFRRSHQUAREARTICLE')  
            await context.dispatch('GetSquareArticle',uid)
       },

     
       //
       
    }
})

